package com.nna88.voz.main;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.nna88.voz.parserhtml.parser;
import com.nna88.voz.quickaction.ActionItem;
import com.nna88.voz.quickaction.QuickAction;
import com.nna88.voz.swipe.SimpleGestureFilter;
import com.nna88.voz.util.UserInfo;


public class Page extends Activity{
	static final String URL="http://vozforums.com/";
	public static final int TASK_GETDOC = 0;
	public static final int TASK_LOGIN = 1;
	public static final int TASK_LOGOUT = 2;
	public static final int TASK_GETQUOTE = 3;
	public static final int TASK_POSTREPLY = 4;
	public static final int TASK_POSTEDIT = 5;
	public static final int TASK_POSTDELETE = 6;
	public static final int TASK_SUBSCRIBE = 7;
	public static final int TASK_UNSUBSCRIBE= 8;
	
	
	public static final int REQUEST_REPLY = 0;
	public static final int REQUEST_QUOTE = 1;
	public static final int REQUEST_EDIT = 2;
	public static final int REQUEST_DELETE =3;
	
	public static final int GO_INDEXPAGE = 0;
	public static final int GO_REFRESH = 1;
	public static final int GO_NEXT = 2;
	public static final int GO_PREVIOUS = 3;
	public static final int GO_FIRST = 4;
	public static final int GO_LAST = 5;
	
	public static final double FONTSCALE_MIN = 0.1;
	public static final double FONTSCALE_MAX = 2.5;
	
	public static int width;
	public static int height;
	
	protected Context mContext;
	protected Activity mActivity;
	protected ListView mList;
	protected ImageButton mMenuSetting;
	protected TextView mMenuTitle;
	protected Document doc;
	protected UserInfo mUser;
	protected static float mTextSize;
	protected TextView mTextNumPage;
	protected RelativeLayout mMenuGoLayout;
	protected TaskGetHtml mTask;
	protected parser mParser;
	protected String sPost;
	protected String sIdThread;
	protected SharedPreferences settings;
	protected static boolean isDisplayGoMenu;

	private SimpleGestureFilter detector;
	private GestureDetector gestureDetector;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.page2);
		super.onCreate(savedInstanceState);
		settings = getSharedPreferences("Setting", MODE_PRIVATE);
		mUser = new UserInfo();
		initUI();
		mMenuTitle.setText("Vozforums");
		getRevolution();
		mParser = new parser(URL);
		mTask = new TaskGetHtml();
		//detector = new SimpleGestureFilter(mActivity,sgl);
		gestureDetector = new GestureDetector(simpleOnGestureListener);

	}

//	public boolean onTouchEvent(MotionEvent event) {
//		 return gestureDetector.onTouchEvent(event);
//	};
	SimpleOnGestureListener simpleOnGestureListener = new SimpleOnGestureListener(){
		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
		   String swipe = "";
		   float sensitvity = 50;
		   // TODO Auto-generated method stub
		   if((e1.getX() - e2.getX()) > sensitvity){
		    swipe += "Swipe Left\n";
		   }else if((e2.getX() - e1.getX()) > sensitvity){
		    swipe += "Swipe Right\n";
		   }else{
		    swipe += "\n";
		   }
		   
		   if((e1.getY() - e2.getY()) > sensitvity){
		    swipe += "Swipe Up\n";
		   }else if((e2.getY() - e1.getY()) > sensitvity){
		    swipe += "Swipe Down\n";
		   }else{
		    swipe += "\n";
		   }
		   toast(swipe);
		   return super.onFling(e1, e2, velocityX, velocityY);
		  }
	};
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		//mTask.cancel(true);
		TaskCancle();
		super.onDestroy();
	}
    @SuppressWarnings("deprecation")
	void getRevolution(){
    	Point size = new Point();
    	WindowManager w = getWindowManager();
	    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
	          w.getDefaultDisplay().getSize(size);
	          width = size.x;
	          height = size.y; 
        }else{
          Display d = w.getDefaultDisplay(); 
          width = d.getWidth(); 
          height = d.getHeight(); 
       }

    }
	public void readSettings() {
		mTextSize = settings.getFloat("FontSize", 1.2F);
		Map<String, String> map  = new HashMap<String, String>();
		map.put("vflastvisit", settings.getString("vflastvisit", ""));
		map.put("vflastactivity", settings.getString("vflastactivity", ""));
		map.put("vfuserid", settings.getString("vfuserid", ""));
		map.put("vfpassword", settings.getString("vfpassword", ""));
		map.put("vfimloggedin", settings.getString("vfimloggedin", ""));
		map.put("vfsessionhash", settings.getString("vfsessionhash", ""));
		mUser.cookies(map);
	}
	public void writeSettingFont(float fontsize){
		SharedPreferences.Editor editor = settings.edit();
		editor.putFloat("FontSize", fontsize);
		editor.commit();
	}
	public void writeSetingUser(Map<String, String> map){
		SharedPreferences.Editor editor = settings.edit();
		editor.putString("vflastvisit", map.get("vflastvisit"));
		editor.putString("vflastactivity", map.get("vflastactivity"));
		editor.putString("vfuserid", map.get("vfuserid"));
		editor.putString("vfpassword", map.get("vfpassword"));
		editor.putString("vfimloggedin", map.get("vfimloggedin"));
		editor.putString("vfsessionhash", map.get("vfsessionhash"));
		editor.commit();
	}
	
	Bitmap loadBitmapAssert(final String url){
		try {
			InputStream is = mContext.getAssets().open(url);
			Bitmap emo = BitmapFactory.decodeStream(is);
			is.close();
			return emo;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			toast("cannot load image assets" + url);
			return null;
		}
	}

	ProgressDialog createProgress(Context mContext){
		 ProgressDialog progressDialog = new ProgressDialog(this);
	     progressDialog.setCancelable(false);
	     progressDialog.setMessage("Loading data......");
	     progressDialog.show();
	     return progressDialog;
	}
	class TaskGetHtml extends AsyncTask<Integer, Void, Integer>{
		ProgressDialog mProgress;
		@Override
		protected Integer doInBackground(Integer... params) {
			// TODO Auto-generated method stub
			switch (params[0]) {
			case TASK_SUBSCRIBE:
	        	Element eleSecurity1 = doc.select("input[name*=securitytoken]").first();
	        	String secToken1 = null;
	        	if(eleSecurity1 != null )secToken1 = eleSecurity1.attr("value");	
				doc = mParser.Subscribe(sIdThread, secToken1);
				break;
			case TASK_UNSUBSCRIBE:
				doc = mParser.UnSubscribe(sIdThread);
				break;
			case TASK_GETDOC:
				doc = mParser.getDoc();
				break;
			case TASK_LOGIN:
				doc = mParser.login(mUser);
				break;
			case TASK_LOGOUT:
				doc = mParser.Logout();
				break;	
			case TASK_GETQUOTE:
				doc = mParser.quoteGet(params[1],params[2]);
				break;
			case TASK_POSTREPLY:
				try {
					Element mainElement =  doc.select("div[align=center]").get(1);
					if(mainElement != null ){
				    	Element eleUrl = mainElement.select("form[action*=newreply.php?do=postreply]").first();
				    	Element eleSecurity = mainElement.select("input[name*=securitytoken]").first();
				    	String url,secToken;
				    	secToken = eleSecurity.attr("value");
				    	if(eleUrl != null){
				    		url = eleUrl.attr("action");
				    		url = url.replace("&amp;","&");
				    		doc = mParser.PostQuickReply(sPost, url, secToken);
				    	}
					}
				} catch (Exception e) {
					// TODO: handle exception
					//toast("Error: cannot Reply");
				}
				break;
			case TASK_POSTEDIT:
				try {
					Element mainElement =  doc.select("div[align=center]").get(1);
			    	//Element eleUrl = mainElement.select("form[action*=editpost.php?do=updatepost]").first();
			    	Element eleSecurity = mainElement.select("input[name*=securitytoken]").first();
			    	Element elePostHash = mainElement.select("input[name*=posthash]").first();
			    	Element elePostStartTime =mainElement.select("input[name*=poststarttime]").first();
			    	Element eleP =mainElement.select("input[name=p]").first();
			    	Element eleTitle =mainElement.select("input[name=title]").first();
			    	String url = null,secToken = null,postHash=null,postStartTime=null,p=null,title=null;
			    	if(eleSecurity != null )secToken = eleSecurity.attr("value");
			    	if(elePostHash != null) postHash = elePostHash.attr("value");
			    	if(elePostStartTime != null) postStartTime = elePostStartTime.attr("value");
			    	if(eleP != null) p = eleP.attr("value");
			    	if(eleTitle != null) title = eleTitle.attr("value");
			    	url = "editpost.php?do=updatepost&p="+p;
			    	doc = mParser.PostEditReply(0,sPost, url, title, secToken, p, postHash, postStartTime);
				} catch (Exception e) {
					// TODO: handle exception
					//toast("Error: cannot Edit");
				}
				break;
			case TASK_POSTDELETE:
				try {
					Element mainElement =  doc.select("div[align=center]").get(1);
			    	Element eleSecurity = mainElement.select("input[name*=securitytoken]").first();
			    	Element eleP =mainElement.select("input[name=p]").first();
			    	Element eleTitle =mainElement.select("input[name=title]").first();
			    	String url = null,secToken = null,p=null,title=null;
			    	if(eleSecurity != null )secToken = eleSecurity.attr("value");
			    	if(eleP != null) p = eleP.attr("value");
			    	if(eleTitle != null) title = eleTitle.attr("value");
			    	url = "editpost.php?do=deletepost&p="+p;
			    	doc = mParser.PostEditReply(1,sPost, url, title, secToken, p, null, null);
				} catch (Exception e) {
					// TODO: handle exception
					//toast("Error: cannot Edit");
				}
				break;
			default:
				break;
			}
			
			return params[0];
		}
		@Override
		protected void onPostExecute(Integer result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			mProgress.dismiss();
			Parser(result);
			switch (result) {
			case TASK_LOGIN:
				if(doc==null)
					toast("Username or Password error!!!");
				else
					writeSetingUser(mUser.cookies());
				break;
			case TASK_LOGOUT:
				writeSetingUser(mUser.cookies());
				break;
			default:
				break;
			}
		}
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			mProgress = createProgress(mContext);
		}
	}
	void Parser(int taskType){
		if((doc==null) && !checkNetwork()){
			alertNetwork();
		}
	}
	boolean checkNetwork(){
	 	ConnectivityManager connections = (ConnectivityManager)this.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = connections.getActiveNetworkInfo();
        if((info != null) && (info.getState() != null) ){
            Toast.makeText(mContext, "connected intetnet", Toast.LENGTH_SHORT).show();
            return true;
        }else{
            Toast.makeText(mContext, "cannot access intetnet", Toast.LENGTH_SHORT).show();
            return false;
        }
	}
	void log(String ss){
		Log.d("nna",ss);
	}
	void toast(String ss){
		Toast.makeText(mContext	,ss, Toast.LENGTH_SHORT).show();
	}
	void toast(String ss,int time){
		if(time==0)		
			Toast.makeText(mContext	,ss, Toast.LENGTH_SHORT).show();
		else 			
			Toast.makeText(mContext	,ss, Toast.LENGTH_LONG).show();
	}
	void toast(String ss,int time, int type){
		Toast to;
		if(time==0)				to = Toast.makeText(mContext,ss, Toast.LENGTH_SHORT);
		else 					to = Toast.makeText(mContext,ss, Toast.LENGTH_LONG);
		if( type == 0)			to.setGravity(Gravity.TOP, 0, 0);
		else if(type == 1 )		to.setGravity(Gravity.CENTER, 0, 0);
		else if(type == 2)		to.setGravity(Gravity.BOTTOM, 0, 0);
		to.show();
	}
	private boolean parseSubscribe(){
		try {
			Element threadtoolElement = doc.select("a[href*=subscription.php]").first();
			String temp = threadtoolElement.attr("href");
			sIdThread = temp.substring(temp.lastIndexOf("=")+1, temp.length());
			//log(sIdThread);
			if(temp.contains("addsubscription"))	return false;
			else return true;
		} catch (Exception e) {
			// TODO: handle exception
			return false;
		}
	}
	protected void TaskCancle(){
		
	}
    String parsePage(int type){
    	Element elePage = doc.select("div[class=pagenav").first();
    	String mIndexPage,temp = null,m_url,idthread=null;
    	int curPage,lastPage,size;
    	Element eleGetId;
    	if(elePage!=null){
			mIndexPage = elePage.select("td[class=vbmenu_control]").text();
    		curPage = Integer.parseInt(mIndexPage.split(" ")[1]);
    		lastPage = Integer.parseInt(mIndexPage.split(" ")[3]);
    		if(mParser.getUrl().contains("showthread.php?p=")){
	    		size = elePage.getElementsByAttribute("href").size();
	    		for(int i=0;i<size;i++){
	    			eleGetId = elePage.getElementsByAttribute("href").get(i);
	    			if(eleGetId != null ){
	        			String temp2 = eleGetId.attr("href");
	        			int startId,endId;
	        			if(temp2.contains("&page=") != true){
	        				temp2 = temp2.concat("&page=1");
	            		}
	        			startId = temp2.indexOf("?t=");
	        			endId = temp2.indexOf("&page");
	        			idthread = temp2.substring(startId+3,endId);
	        			break;
	        		}
	    		}
	    		m_url = URL+"showthread.php?t="+idthread+"&page="+String.valueOf(curPage);
	    		log(m_url);
    		}else{
    			 m_url = mParser.getUrl();
                 if(m_url.contains("&page=") != true){
                         m_url = m_url.concat("&page=1");
                 }
    		}
    			
    		switch (type) {
			case GO_INDEXPAGE:
				temp = mIndexPage;
				break;
			case GO_FIRST:
				temp = m_url.split("&page")[0];
				break;
			case GO_NEXT:
				temp = m_url.substring(0,m_url.lastIndexOf("=")+1).concat(String.valueOf(curPage+1));
				break;
			case GO_PREVIOUS:
				temp = m_url.substring(0,m_url.lastIndexOf("=")+1).concat(String.valueOf(curPage-1));
	    		if(temp.contains("&page=0")){
	    			temp = temp.split("&page")[0];
	    		}
	    		break;
			case GO_LAST:
	    		temp = m_url.split("&page")[0];
	    		temp = temp.concat("&page=" + String.valueOf(lastPage));
	    		break;
			case GO_REFRESH:
				temp = m_url;
				break;
			}
    		return temp;
    	}else
    		return null;

    }
    private void GoPage(){

		// TODO Auto-generated method stub
		mMenuGoLayout = (RelativeLayout)findViewById(R.id.panel_go);
		if(isDisplayGoMenu)	mMenuGoLayout.setVisibility(View.VISIBLE);
		else				mMenuGoLayout.setVisibility(View.GONE);
		if(!mContext.getClass().getName().equals("com.nna88.voz.main.Page1")){
			mMenuGoLayout.setVisibility(View.GONE);	
		}
		mTextNumPage = (TextView)findViewById(R.id.Go_NumPage);
		ImageButton mGo = (ImageButton)findViewById(R.id.Page2Page);
		ImageButton mNext = (ImageButton)findViewById(R.id.Page2Next);
		ImageButton mFirst = (ImageButton)findViewById(R.id.Page2First);
		ImageButton mPrev = (ImageButton)findViewById(R.id.Page2Previous);
		ImageButton mLast = (ImageButton)findViewById(R.id.Page2Last);
		
		mGo.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				TaskCancle();
				mTask = new TaskGetHtml();
				mTask.execute(TASK_GETDOC);
			}
		});
		mNext.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODiO Auto-generated method stub
				toast(parsePage(GO_NEXT));
				String temp = parsePage(GO_NEXT);
				if(temp != null){
					mParser.setUrl(parsePage(GO_NEXT));
					//mTask.cancel(true);
					TaskCancle();
					mTask = new TaskGetHtml();
					mTask.execute(TASK_GETDOC);
				}
			}
		});
		mFirst.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String temp = parsePage(GO_FIRST);
				if(temp != null){
					mParser.setUrl(parsePage(GO_FIRST));
					//mTask.cancel(true);
					TaskCancle();
					mTask = new TaskGetHtml();
					mTask.execute(TASK_GETDOC);
				}
			}
		});
		mPrev.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String temp = parsePage(GO_PREVIOUS);
				if(temp != null){
					mParser.setUrl(parsePage(GO_PREVIOUS));
					//mTask.cancel(true);
					TaskCancle();
					mTask = new TaskGetHtml();
					mTask.execute(TASK_GETDOC);
				}
			}
		});
		mLast.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String temp = parsePage(GO_LAST);
				if(temp != null){
					mParser.setUrl(parsePage(GO_LAST));
					//mTask.cancel(true);
					TaskCancle();
					mTask = new TaskGetHtml();
					mTask.execute(TASK_GETDOC);
				}
			}
		});
    }
    void ActionPopupSetting(final View arg1,final int pos){
	    QuickAction mQuickAction 	= new QuickAction(mContext);
	    if(mUser.isLogin()){
			mQuickAction.addActionItem(new ActionItem(1, "User", getResources().getDrawable(R.drawable.login_on)));
		}else{
			mQuickAction.addActionItem(new ActionItem(1, "User", getResources().getDrawable(R.drawable.login)));
		}
		mQuickAction.addActionItem(new ActionItem(2, "Font", getResources().getDrawable(R.drawable.font)));
		
		if(!mContext.getClass().getName().equals("com.nna88.voz.main.Page1")){
			mQuickAction.addActionItem(new ActionItem(3, "Page",
					getResources().getDrawable(R.drawable.menu_go)));	
		}
		mQuickAction.show(arg1);
		mQuickAction.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
			@Override
			public void onItemClick(QuickAction quickAction, int position, int actionId) {
				//toast("actionId:"+String.valueOf(actionId)+";"+"position:"+String.valueOf(pos));
				if(actionId==1){
					if(mUser.isLogin()){
						ActionPopupUser(arg1, 0);
					}else{
						alertLogin();
					}
				}else if(actionId == 2){
					alertFont();
				}else if(actionId == 3){
					if(mMenuGoLayout.getVisibility() == View.GONE){
						isDisplayGoMenu = true;
						mMenuGoLayout.setVisibility(View.VISIBLE);
					}else{
						isDisplayGoMenu = false;
						mMenuGoLayout.setVisibility(View.GONE);
					}
				}
			}
		});
	}
	void initUI(){
		mContext = this;
		mActivity = this;
		mMenuSetting = (ImageButton)findViewById(R.id.menu_set);
		mMenuTitle = (TextView)findViewById(R.id.menu_title);
		mList = (ListView)findViewById(R.id.mainlist);
		GoPage();
		mMenuSetting.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				ActionPopupSetting(v, 0);
			}
		});
	}
	void ActionPopupUser(final View arg1,final int pos){
		if(arg1 == null)			log("arg null");
		ActionItem replyItem 	= new ActionItem(1, "Logout", getResources().getDrawable(R.drawable.login));
		ActionItem quoteItem 	= new ActionItem(2, "User CP", getResources().getDrawable(R.drawable.menu_usercp));
	    QuickAction mQuickAction 	= new QuickAction(mContext);
		mQuickAction.addActionItem(replyItem);
		mQuickAction.addActionItem(quoteItem);
		
		if(mContext.getClass().getName().equals("com.nna88.voz.main.Page3")){
			if(!parseSubscribe()){
				mQuickAction.addActionItem(new ActionItem(3, "Subscribe", 
						getResources().getDrawable(R.drawable.subscribe_on)));
			}else{
				mQuickAction.addActionItem(new ActionItem(4, "UnSubscribe", 
						getResources().getDrawable(R.drawable.subscribe_off)));
			}
		}
			
		mQuickAction.show(arg1);
		mQuickAction.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
			@Override
			public void onItemClick(QuickAction quickAction, int position, int actionId) {
				//toast("actionId:"+String.valueOf(actionId)+";"+"position:"+String.valueOf(pos));
				if(actionId==1){
					toast("log out");
					//mTask.cancel(true);
					TaskCancle();
					mTask = new TaskGetHtml();
					mTask.execute(TASK_LOGOUT);
					
				}else if(actionId ==2){
					TaskCancle();
					Intent i = new Intent(mContext, PageCP.class);
					startActivity(i);
				}else if(actionId == 3){
					//toast("S");
					//mTask.cancel(true);
					TaskCancle();
					mTask = new TaskGetHtml();
					mTask.execute(TASK_SUBSCRIBE);
					
				}else if(actionId == 4){
					//toast("S");
					//mTask.cancel(true);
					TaskCancle();
					mTask = new TaskGetHtml();
					mTask.execute(TASK_UNSUBSCRIBE);
					
				}
			}
		});
	}
	protected Dialog alertFont(){
		final Dialog dialog = new Dialog(mContext);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.setfont);
		WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
	    lp.copyFrom(dialog.getWindow().getAttributes());
	    lp.width = WindowManager.LayoutParams.FILL_PARENT;
	    lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
		dialog.getWindow().setAttributes(lp);
		final TextView text1 = (TextView)dialog.findViewById(R.id.alert_txt1);
		text1.setGravity(Gravity.CENTER);
		SeekBar sb = (SeekBar)dialog.findViewById(R.id.alert_seekbar);
		sb.setMax(100);
		sb.setProgress((int)(100*(mTextSize-FONTSCALE_MIN)/
				(FONTSCALE_MAX-FONTSCALE_MIN)));
		text1.setText(String.valueOf(sb.getProgress()));
	    sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
	        public void onProgressChanged(SeekBar seekBar, int progress, 
	        		boolean fromUser){
	        	text1.setText(String.valueOf(progress));
	        	mTextSize = (float)((FONTSCALE_MAX-FONTSCALE_MIN)/
	        			(100-0)*(progress) + FONTSCALE_MIN);
	        	updateSetFont(mTextSize);
	        	
	        }
			@Override
			public void onStartTrackingTouch(SeekBar arg0) {
				// TODO Auto-generated method stub
			}
			@Override
			public void onStopTrackingTouch(SeekBar arg0) {
				// TODO Auto-generated method stub
				//toast("textSize:" + mTextSize);
				writeSettingFont(mTextSize);
			}
	    });
		dialog.show();
		return dialog;
	}
	
	protected void updateSetFont(float value){
		
	}
	protected Dialog alertNetwork(){
		final Dialog dialog = new Dialog(mContext);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.login);
		final EditText edit1 = (EditText)dialog.findViewById(R.id.alert_edit1);
		final EditText edit2 = (EditText)dialog.findViewById(R.id.alert_edit2);
		TextView textTitle = (TextView)dialog.findViewById(R.id.alert_title);
		TextView text1 = (TextView)dialog.findViewById(R.id.alert_txt1);
		TextView text2 = (TextView)dialog.findViewById(R.id.alert_txt2);
		Button buttonOK = (Button)dialog.findViewById(R.id.alert_ok);
		textTitle.setText("Network");
		edit1.setVisibility(View.GONE);
		edit2.setVisibility(View.GONE);
		text1.setText("Check the network!!!");
		text2.setVisibility(View.GONE);
		buttonOK.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//mTask.cancel(true);
				TaskCancle();
				mTask = new TaskGetHtml();
				dialog.dismiss();
			}
		});
		dialog.show();
		return dialog;
	    
	}
	protected Dialog alertLogin(){
		final Dialog dialog = new Dialog(mContext);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.login);
		final EditText edit1 = (EditText)dialog.findViewById(R.id.alert_edit1);
		final EditText edit2 = (EditText)dialog.findViewById(R.id.alert_edit2);
		TextView textTitle = (TextView)dialog.findViewById(R.id.alert_title);
		TextView text1 = (TextView)dialog.findViewById(R.id.alert_txt1);
		TextView text2 = (TextView)dialog.findViewById(R.id.alert_txt2);
		Button buttonOK = (Button)dialog.findViewById(R.id.alert_ok);
		final boolean isLogin;
		if(mUser.isLogin()){
			isLogin = true;
			textTitle.setText("LogOut");
			edit1.setVisibility(View.GONE);
			edit2.setVisibility(View.GONE);
			text1.setText("UserName:"+mUser.User());
			text2.setVisibility(View.GONE);
		}else{
			isLogin = false;
			textTitle.setText("Login");
			edit1.setVisibility(View.VISIBLE);
			edit2.setVisibility(View.VISIBLE);
			text2.setVisibility(View.VISIBLE);
			text1.setText("UserName:");
		}
		
		buttonOK.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//mTask.cancel(true);
				TaskCancle();
				mTask = new TaskGetHtml();
				if(!isLogin){
					//toast("Click OK"+edit1.getText()+";"+edit2.getText());
					mUser.add(edit1.getText().toString(), edit2.getText().toString());
					mTask.execute(TASK_LOGIN);
				}else{
					mUser.add(edit1.getText().toString(), edit2.getText().toString());
					mTask.execute(TASK_LOGOUT);
				}
				dialog.dismiss();
			}
		});
		dialog.show();
		return dialog;
	    
	}

  
}
